﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;

namespace DAL
{
    class SampleServive

    {

        
        CRMEntities db;

        public List<SampleEntity> getSampleSachet()
        {
            db = new CRMEntities();
            var query = from r in db.Samples
                        where r.Parent_Code == (SharedData.SampleList.Where(a => a.Sample_Code == "SA").SingleOrDefault().Parent_Code)
                        select new DTO.SampleEntity
                        {
                            Sample_ID = r.ID,
                            Sample_Code = r.Sample_Code,
                            SampleName = r.SampleName,
                            Sample_Level = r.SampleLevel,
                            Parent_Code = r.Parent_Code,
                            Description = r.Description
                        };
            return query.ToList();
        }

        public List<SampleEntity> getSampleBooklet()
        {
            db = new CRMEntities();
            var query = from r in db.Samples
                        where r.Parent_Code == (SharedData.SampleList.Where(a => a.Sample_Code == "BO").SingleOrDefault().Parent_Code)
                        select new DTO.SampleEntity
                        {
                            Sample_ID = r.ID,
                            Sample_Code = r.Sample_Code,
                            SampleName = r.SampleName,
                            Description = r.Description,
                            Sample_Level = r.SampleLevel,
                            Parent_Code = r.Parent_Code,
                        };
            return query.ToList();
        }

        public List<SampleEntity> LoadListSample()
        {
            db = new CRMEntities();
            var query = (from sa in db.Samples
                         orderby sa.ID
                         select new SampleEntity
                         {
                             Sample_ID = sa.ID,
                             Sample_Code = sa.Sample_Code,
                             SampleName = sa.SampleName,
                             Sample_Level = sa.SampleLevel,
                             Description = sa.Description,
                             Parent_Code = sa.Parent_Code
                         }).ToList();
            return query;
        }

        public void UpdateSample(Sample _sample)
        {
            CRMEntities db = new CRMEntities();
            var query = (from sa in db.Samples
                         where sa.ID == _sample.ID
                         select sa).SingleOrDefault();
            if (query != null)
            {
                query = _sample;
            }

            db.SaveChanges();
            db.Dispose();

        }


        public void AddSample(String _code, String _name, int _level, String _dct, int _pCode)
        {
            CRMEntities db = new CRMEntities();

            Sample _sample = new Sample();
            _sample.SampleName = _name;
            _sample.SampleLevel = _level;
            _sample.Description = _dct;
            _sample.Parent_Code = _pCode;

            db.Samples.Add(_sample);
            db.SaveChanges();
            db.Dispose();

        }
    
    }
}
